// JavaScript Document

function addQueryRow(tableID)
{
	counter = document.getElementById("criteria_counter").value;
		var table  =  document.all ? document.all[tableID] : document.getElementById(tableID);
		if (document.all || document.getElementById)
		{
			var row  = table.insertRow(table.rows.length-2);
			var cell1 = row.insertCell(0);
			var cell2 = row.insertCell(1);
			var cell3 = row.insertCell(2);
			var cell4 = row.insertCell(3);
			var cell5 = row.insertCell(4);
			var cell6 = row.insertCell(5);
			var cell7 = row.insertCell(6);
			var cell8 = row.insertCell(7);
			var cell9 = row.insertCell(8);
			var cell10 = row.insertCell(9);
			
					
			row.setAttribute( 'id' , 'query'+counter);
			cell1.innerHTML = '<td class="Labels">Field</td>';
			
			var str = '<select name="combo_field[]" id="combo_field'+counter+'" class="InputComboList">';
			
			for(i=0; i<document.getElementById("combo_field0").options.length; i++) {
				
				str += "<option value=\"" + document.getElementById("combo_field0").options[i].value +"\">" +
				        document.getElementById("combo_field0").options[i].text+"</option>";
			   
			}
			str += "</select>";
			cell2.innerHTML  = str;
			
			cell3.innerHTML = '<td class="Labels" >Alias</td>';			
			cell4.innerHTML  = '<input name="text_alias[]" type="text" class="InputText" id="text_alias'+counter+'" >';

			cell5.innerHTML = '<td class="Labels" >Sort</td>';
			var str2 = '<select name="combo_sort[]" id="combo_sort'+counter+'" class="InputComboList">';
			
			for(i=0; i<document.getElementById("combo_sort0").options.length; i++) {
				
				str2 += "<option value=\"" + document.getElementById("combo_sort0").options[i].value +"\">" +
				        document.getElementById("combo_sort0").options[i].text+"</option>";
			   
			}
			str2 += "</select>";
			cell6.innerHTML  = str2;
			
			cell7.innerHTML = '<td class="Labels" >Criteria</td>';			
			cell8.innerHTML  = '<input name="text_criteria[]" type="text" class="InputText" id="text_criteria'+counter+'" >';
			
			cell9.innerHTML  = '<input type="button" name="btn_Add" id="'+'btn_Add'+counter+'" class="InputButton" value="Add" onClick="addQueryRow(\'table1\');" ></td>';
			cell10.innerHTML  = '<input type="button" name="'+'query'+counter+'_btn" id="'+'query'+counter+'_btn" class="InputButton" value="Remove" onClick="removeCriteriaRow(\'table1\',this);" ></td>';

	counter++;
	document.getElementById("criteria_counter").value= counter;	

	}
	
}

function loadQuery(){
	
		var strMain="";
		var strTables="";
		var strFields="";
		var arrAlias=new Array();
		var strSort="";
		var strCriteria="";
		var temp;
		
		var ob = document.form1.tblName;
		var j=0;
		for(temp=0;temp<ob.options.length;temp++){

			if (ob.options[temp].selected) {

				if(strTables!=""){
					strTables +=",";		
				}
				strTables += ob.options[temp].value;
			}
		}
		var counter = document.getElementById("criteria_counter").value;
		
		for(var i=0; i<counter; i++){
			
			
				if(strMain!=""){
					strMain+=",";		
				}

					strMain+=" ";
					strMain+=document.getElementById("combo_field"+i).value;

				if(document.getElementById("text_alias"+i).value!=""){
					strMain+=" as ";
					strMain+=document.getElementById("text_alias"+i).value;
					arrAlias[j]=document.getElementById("text_alias"+i).value;
					j++;
				}else {
					arrAlias[j]=document.getElementById("combo_field"+i).value;
					j++;
				}
				
				if(document.getElementById("combo_sort"+i).value!=0){
					
					if(strSort!=""){
						strSort +=",";	
					}
					strSort += document.getElementById("combo_field"+i).value;
					strSort += " ";
					strSort += document.getElementById("combo_sort"+i).value;
				}
				
				if(document.getElementById("text_criteria"+i).value!=""){
					
					if(strCriteria!=""){
						strCriteria +=" and ";	
					}
					
					strCriteria += document.getElementById("combo_field"+i).value;
					strCriteria += " = ";
					strCriteria += document.getElementById("text_criteria"+i).value;
					
				}
		}
	if(strCriteria!=""){
			strCriteria = " where "+strCriteria;
	}
	if(strSort!=""){
			strSort = " order by "+strSort;
	}
	var newString = "Select "+strMain+" from "+strTables+" "+strCriteria+" "+strSort;
	
	document.getElementById("text_query").value = newString;
//	document.getElementById("btn_report").type="button";	
	document.getElementById("btn_report").disabled=false;
	document.getElementById("arrAlias").value=arrAlias;
}


function submitReport(){
		document.getElementById("form1").action="GeneratedReport.php";
		document.getElementById("form1").submit();
}

var xmlHttp;

function generateReport(){

		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
			  alert ("Your browser does not support AJAX!");
			  return;
		  } 
		  
		 var url="../../controller/ControlManagement.php"
		 url=url+"?param=reportEngine"
		 url=url+"&option=report"
		 url=url+"&sid="+Math.random()
		 xmlHttp.onreadystatechange=queryStateChanged 
	 	 xmlHttp.open("POST",url,true)
		 
		var params="strQuery=";
		params += document.getElementById("text_query").value;
		params +="&";
		params +="counter";
		params +=document.getElementById("criteria_counter").value;	
		
		 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	     xmlHttp.setRequestHeader("Content-length", params.length);
  	     xmlHttp.setRequestHeader("Connection", "close");
		 xmlHttp.send(params)	
		
}

function queryStateChanged(){
	
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
			myRef = window.open(self.location+'/GenerateReport.php','mywin','left=20,top=20,width=500,height=500,toolbar=1,resizable=0');
	}
}


function loadTableSet(){
	
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		  {
			  alert ("Your browser does not support AJAX!");
			  return;
		  } 
		  
		 var url="../../controller/ControlManagement.php"
		 url=url+"?param=reportEngine"
		 url=url+"&option=tables"
		 url=url+"&sid="+Math.random()
		 xmlHttp.onreadystatechange=tableStateChanged 
	 	 xmlHttp.open("POST",url,true)
		
		var params="tblName=";
		var ob = document.form1.tblName;
		
		if(ob.selectedIndex != -1){
			
		for (var i = 0; i < ob.options.length; i++) {
		
			if (ob.options[i].selected) {
				
				if(i!=0){
					params +=",";		
				}
				params += encodeURI(ob.options[i].value);
			}
		
		}
  
		 xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	     xmlHttp.setRequestHeader("Content-length", params.length);
  	     xmlHttp.setRequestHeader("Connection", "close");
		 xmlHttp.send(params)	
		}
		
}

function tableStateChanged() 
{ 

	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		xmlDoc=xmlHttp.responseXML;		 
 
		var counter = document.getElementById("criteria_counter").value;
		var str="";
		for(var i=0;i<counter;i++){
			str="combo_field"+i;
			
			var combo = document.getElementById(str);
			combo.options.length=0;
			var arr = xmlHttp.responseText.split(",");
			var length = arr.length;
			
			for(var j=0;j<length;j++){
				combo.options[j]= new Option(arr[j],arr[j]);
			}
			
		
		}
		
	 }

} 


function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
